import api from '../../api';
import { message } from 'antd';
import { forEach } from 'lodash';

export default {
  namespace: 'creditSettingM',
  state: {
    portfolioList: [], //组合下拉列表
    shortName: [], //短名下拉列表
  },
  effects: {
    // 删除
    *deleteCredit({ payload, callback }, { call, put, select }) {
      const res = yield call(api.creditSetting.deleteCredit, payload);
      if (res) {
        if (res.code == 0) {
          message.success(res.msg || '删除成功！');
          callback('success');
        } else {
          message.error(res.msg || '删除失败！');
        }
      } else {
        message.error('删除失败！');
      }
    },
    //查询授信设置详情
    *getCreditDetail({ payload, callback }, { call, put, select }) {
      let res;
      if (payload.type) {
        //审批流查询详情
        res = yield call(api.creditSetting.getCreditDetailProcess, payload);
      } else {
        res = yield call(api.creditSetting.getCreditDetail, payload);
      }
      if (res) {
        if (res.code == 0) {
          callback(res.data);
        } else {
          callback({});
          message.error(res.msg || '查询授信设置详情失败');
        }
      } else {
        callback({});
        message.error('查询授信设置详情失败');
      }
    },
    //查询额度设置列表
    *getCreditList({ payload, callback }, { call, put, select }) {
      const res = yield call(api.creditSetting.getCreditList, payload);
      if (res) {
        if (res.code == 0) {
          callback(res.data);
        } else {
          yield put({
            type: 'save',
            payload: {
              tableData: [],
            },
          });
          message.error(res.msg || '查询列表失败!');
        }
      } else {
        yield put({
          type: 'save',
          payload: {
            tableData: [],
          },
        });
        message.error('查询列表失败!');
      }
    },
    // 新增授信设置
    *addCredit({ payload, callback }, { call, put, select }) {
      const res = yield call(api.creditSetting.addCredit, payload);
      if (res) {
        if (res.code == 0) {
          callback(res.msg || '新增成功');
        } else {
          callback();
          message.error(res.msg || '新增失败！');
        }
      } else {
        callback();
        message.error('新增失败！');
      }
    },
    // 编辑授信设置
    *updateCredit({ payload, callback }, { call, put, select }) {
      let res;
      if (payload.type) {
        //审批流查询详情
        res = yield call(api.creditSetting.updateCreditProcess, payload);
      } else {
        res = yield call(api.creditSetting.updateCredit, payload);
      }
      if (res) {
        if (res.code == 0) {
          callback(res.msg || '修改成功');
        } else {
          callback();
          message.error(res.msg || '修改失败！');
        }
      } else {
        callback();
        message.error('修改失败！');
      }
    },
    //模糊查询短名
    *getShortName({ payload, callback }, { call, put, select }) {
      const res = yield call(api.creditSetting.getShortName, payload);
      if (res && res.code == 0) {
        yield put({
          type: 'save',
          payload: {
            shortName: res.data,
          },
        });
      } else {
        message.error('查询短名失败!');
      }
    },
  },
  reducers: {
    save(state, { payload }) {
      return { ...state, ...payload };
    },
  },
};
